
import { StaticGroup } from "../model";
import AreaJson from '../json/AreaJson.json'
import AreaConfig from '../json/AreaConfig.json'
export class InspectorService {
    public setInspectorAttributes(options) {
        StaticGroup.options=options;
        this.getAreaInfo(options.base_adcode);

    }

    getAreaInfo(baseAdcode) {
        const leve = ["nation", "province", "city", "district"];

        const names = baseAdcode.split(',');
        let acroutes = [] as any;
        let adcode = 0;
        let path = "world";
        let lev_1 = 0;

        for (let i = 0; i < names.length; i++) {
            const ar = this.requestArea(names[i], adcode);
            if (ar) {
                acroutes = ar.acroutes;
                adcode = ar.adcode;
            } else {
                break;
            }
        }
        if (adcode == 0) {
            adcode = 440000;
            acroutes=[100000];
        }
        acroutes.forEach(x => {
            const areac = AreaJson.find(xr => xr.adcode == x);
            if (areac) {
                path += "/" + areac.name
            }
        })
        const areaInfo = AreaJson.find(xr => xr.adcode == adcode);
        path += "/" + areaInfo.name;
        lev_1 = leve.indexOf(areaInfo.level);
        const config = AreaConfig.find(x => x.adcode == areaInfo.adcode);
        StaticGroup.areaInfo = areaInfo;
        StaticGroup.adcode = areaInfo.adcode;
        StaticGroup.areaConfig = config ? config : {
            "adcode": areaInfo.adcode,
            "scale": this.getScale(areaInfo.level),
            "width": 17,
            "depth": 0.5,
            "rotpos": {"x": 0, "z": 0}
        }
        //json请求对象
        StaticGroup.paraData={
            "level": lev_1+1,
            "conditions": [path]
        }
    }
    requestArea(key,parent) {
        if (key.trim() != "") {
            const area = AreaJson.find(x => x.name == key && (parent == 0 ? true : x.parent == parent));
            return area;
        }
        return null;
    }



    private getScale(type){
        if (type==='province'){
            return 120;
        }else if (type==='city'){
            return 330;
        }else if (type==='district'){
            return 550;
        }
    }

}